package com.hnichr.ump.api.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.hnichr.ump.common.salon.entity.Applicants;
import com.hnichr.ump.common.salon.entity.SalonInfo;
import com.hnichr.ump.common.salon.service.ApplicantsService;
import com.hnichr.ump.common.salon.service.SalonInfoService;
import com.hnichr.ump.common.vo.SalonInfoVO;
import com.mxpioframework.common.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "SalonApiController", description = "沙龙活动接口")
@RestController
@RequestMapping("/api/salon/")
public class SalonApiController {

    @Autowired
    private SalonInfoService salonInfoService;

    @Autowired
    private ApplicantsService applicantsService;

    @GetMapping("detail/{id}")
    @Operation(summary = "沙龙活动详情", description = "沙龙活动详情", method = "GET")
    public Result<SalonInfo> detail(@Parameter(description="ID") @PathVariable(name = "id", required = true) String id){
        SalonInfo salonInfo = salonInfoService.getById(id);
        return Result.OK(salonInfo);
    }


    @PostMapping("apply")
    @Operation(summary = "报名", description = "报名", method = "POST")
    public Result<Applicants> apply(@RequestBody Applicants applicant) {
        applicantsService.apply(applicant);
        return Result.OK(applicant);
    }


    @GetMapping("application/{mobile}")
    @Operation(summary = "根据手机号查询所有报名信息", description = "根据手机号查询所有报名信息", method = "GET")
    public Result<List<SalonInfoVO>> getApplication(@Parameter(description="mobile") @PathVariable(name = "mobile", required = true) String mobile) {
        List<Applicants> list = applicantsService.listByMobile(mobile);
        return Result.OK(getSalonInfoVOList(list));
    }

    /**
     * 出参转换
     * @param list
     * @return
     */
    private  List<SalonInfoVO> getSalonInfoVOList(List<Applicants> list){
        List<SalonInfoVO> salonInfoVOList = new ArrayList<>();
        if(CollUtil.isEmpty(list)){
            return salonInfoVOList;
        }
        for (Applicants applicants: list) {
            SalonInfoVO salonInfoVO = new SalonInfoVO();
            BeanUtil.copyProperties(applicants.getSalonInfo(),salonInfoVO);
            //审核状态 0-未审核  1-审核通过 2-审核未通过
            salonInfoVO.setApplicateinStatus(applicants.getStatus() == "1"? "审核通过":applicants.getStatus() == "2"?"审核未通过":"未审核");
            salonInfoVO.setName(applicants.getName());
            salonInfoVOList.add(salonInfoVO);
        }
        return salonInfoVOList;
    }


}
